package org.compression.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.compression.thread.PdfCom;
import org.springframework.stereotype.Service;


@Service
public class SearchTxtServiceUtil {

	public static Object compare(String str,File pdfFile,int startPage,int endPage) throws IOException {
		HashMap<Object,String> hashPage=new HashMap<>();

		// 是否排序
		boolean sort = false;
		
		// 内存中存储的PDF Document
		PDDocument document = null;

		File outputFile = null;
		try {
			document = PDDocument.load(pdfFile);
			
			PDFTextStripper stripper = null;
			stripper = new PDFTextStripper();
			// 设置是否排序
			stripper.setSortByPosition(sort);
			// 设置起始页
			stripper.setStartPage(startPage);
			// 设置结束页
			stripper.setEndPage(endPage);
			String tempContext=stripper.getText(document);
			 tempContext=tempContext.replaceAll("\r\n", "");
			if(tempContext.indexOf(str)!=-1) {
				return hashPage.put(startPage, tempContext);
			}
			
			
		}catch (Exception e) {
			e.printStackTrace();
			return "error";
		}finally {
			if (document != null) {
				// 关闭PDF Document
				document.close();
			}
		}
		return "";
	}

	
	
	
	public static Object compare(String str,File pdfFile,int startPage) throws IOException {
		return compare(str,pdfFile,startPage,startPage);
	}
	
	
	
	
	
	
	
	
	
	
	
	public static String path="D:\\company\\葛朗台信息有限公司\\source";

	public static Object getDataSource(String str) throws Exception{
		HashMap<Object,String> hashPage=new HashMap<>();
		File file=new File(path);
		File[] files=file.listFiles();
		for(File d:files) {
			//System.out.println(d.getName());
			if(d.getName().indexOf("pdf")!=-1) {
				int pages=PdfUtil.getNumberOfPages(d.getPath());
				hashPage.put(pages,d.getPath());
			}
		}
		return hashPage;
	}











	//public static void main(String args[]) throws Exception {
	public static Object tss() throws Exception{
		 
		 ExecutorService pool = Executors.newFixedThreadPool(20);
		 File file=new File("D:\\company\\葛朗台信息有限公司\\source\\Cell-17-03-09.pdf");
		HashMap<Object,String> hashPage=(HashMap<Object, String>) getDataSource("adsasd");
		 for (Object key : hashPage.keySet()) {
			System.out.println("开始时间："+System.currentTimeMillis()); 
			  for (int i=1; i<=(int)key; i++){
				  Object str=  compare("pro--rich P domain",new File("D:\\company\\葛朗台信息有限公司\\source\\Cell-17-03-09.pdf"),i);
				  System.out.println(str);
			  }
				System.out.println("开始时间："+System.currentTimeMillis()); 
		/*//	  System.out.println("key= "+ key + " and value= " + hashPage.get(key));
			  for (int i=1; i<=(int)2; i++){
		         //   pool.execute(new PdfCom(i, "pro--rich P domain",file));
			  
				// new  Thread(new PdfCom(i, "pro--rich P domain",file)).start();
			  }*/
			 }
		//Object str=compare("pro--rich P domain",new File("D:\\company\\葛朗台信息有限公司\\source\\Cell-17-03-09.pdf"),2);
	
		
	//	System.out.println(str);
		    return  hashPage;
	}




}
